package Arrays;

public class _724_FindPivotIndex {
    //tips:think of the number that under 0

    public int pivotIndex(int[] nums) {
        if(nums.length==0 || nums.length==1) return -1;
        int pivotIndex = -1;
        int leftSum = nums[0];
        int rightSum = nums[nums.length-1];
        int left = 0,right = nums.length-1;
        while (left != right) {
            if (leftSum > rightSum) {
                right--;
                rightSum+=nums[right];
            } else if (leftSum < rightSum) {
                left++;
                leftSum += nums[left];
            } else {
                right--;
                rightSum+=nums[right];
            }
        }
        if(leftSum==rightSum){
            pivotIndex = left;
        }
        return pivotIndex;
    }

    //solution using sum
    //don't think about wheather the leftsum is larger or sumaller than the rightsum,
    //just find out if there is a pivot index
    public int pivotIndex1(int[] nums) {
        int sum = 0;
        int leftSum = 0;
        for (int i : nums) {
            sum+=i;
        }
        for(int i=0;i<nums.length;i++) {
            if (leftSum == sum - leftSum - nums[i]) {
                return i;
            } else {
                leftSum+=nums[i];
            }
        }
        return -1;
    }
}
